using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ERP.ERPSystem.WarehouseManagement;
using ERP.ERPSystem.WarehouseManagement.Dto;
using Microsoft.AspNetCore.Mvc;

namespace ERP.Web.Controllers
{
    /// <summary>
    /// 节假日设置控制器
    /// </summary>
    public class HolidaysSetController : Controller
    {
        private readonly IHolidaysSetServices _holidaysSetServices;

        public HolidaysSetController(IHolidaysSetServices holidaysSetServices)
        {
            _holidaysSetServices = holidaysSetServices;
        }

        /// <summary>
        /// 节假日设置列表页面
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 节假日设置表单页面
        /// </summary>
        public IActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 获取节假日设置分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetPagedList([FromForm] HolidaysSetQueryDto query)
        {
            try
            {
                var result = await _holidaysSetServices.GetPagedListAsync(query);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "查询失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取节假日设置详情
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                var result = await _holidaysSetServices.GetByIdAsync(id);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "获取详情失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 创建节假日设置
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Create([FromForm] HolidaysSetCreateDto createDto)
        {
            try
            {
                // 添加调试日志
                Console.WriteLine($"收到的数据 - HolidaysTime: {createDto.HolidaysTime}, HolidaysType: {createDto.HolidaysType}");
                
                // 解析日期并打印调试信息
                var parsedDate = createDto.GetHolidaysTime();
                Console.WriteLine($"解析后的日期: {parsedDate:yyyy-MM-dd HH:mm:ss} (Kind: {parsedDate.Kind})");
                
                var result = await _holidaysSetServices.CreateAsync(createDto);
                
                // 直接返回简化的响应格式，确保前端能够正确识别
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"创建节假日设置失败: {ex.Message}");
                Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
                return Json(new { success = false, message = "创建失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 更新节假日设置
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Update([FromForm] HolidaysSetUpdateDto updateDto)
        {
            try
            {
                var result = await _holidaysSetServices.UpdateAsync(updateDto);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "更新失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 删除节假日设置
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                var result = await _holidaysSetServices.DeleteAsync(id);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除节假日设置
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> BatchDelete([FromForm] List<int> ids)
        {
            try
            {
                var result = await _holidaysSetServices.BatchDeleteAsync(ids);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "批量删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 获取指定年月的节假日设置
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetByYearMonth(int year, int month)
        {
            try
            {
                Console.WriteLine($"获取节假日数据: {year}年{month}月");
                var result = await _holidaysSetServices.GetByYearMonthAsync(year, month);
                
                // 添加调试信息
                Console.WriteLine($"查询到 {result.Data?.Count ?? 0} 条记录");
                if (result.Data != null && result.Data.Count > 0)
                {
                    var firstRecord = result.Data[0];
                    Console.WriteLine($"第一条记录: ID={firstRecord.Id}, Date={firstRecord.HolidaysTime:yyyy-MM-dd HH:mm:ss}, Type={firstRecord.HolidaysType}");
                    Console.WriteLine($"DateString={firstRecord.HolidaysTimeString}, DateIso={firstRecord.HolidaysTimeIso}");
                }
                
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取节假日设置失败: {ex.Message}");
                return Json(new { success = false, message = "获取节假日设置失败：" + ex.Message });
            }
        }
    }
} 